package com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skytech.tax.customsdatawarehouse.common.util.SecurityUtil;
import com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.dao.DictCategoryDao;
import com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.entity.DictCategory;
import com.skytech.tax.customsdatawarehouse.singlewindow.dictcategory.domain.entity.DictCategoryItem;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DictCategoryService {
    private static final Logger LOGGER = LoggerFactory.getLogger(DictCategoryService.class);

    @Resource
    private DictCategoryDao dictCategoryDao;

    @Value("${customsdatawarehouse.token}")
    private String token;

    public void save(String categoryList) {
        if (StringUtils.isEmpty(categoryList)) return;
        String categoryListText = SecurityUtil.decrypt3DES(token, categoryList);
        try {
            JSONObject categoryObj = JSON.parseObject(categoryListText);
            if (categoryObj == null) return;
            JSONArray categoryArr = categoryObj.getJSONArray("categoryList");
            if (CollectionUtils.isEmpty(categoryArr)) return;

            //字典列表拆分并存储
            List<DictCategory> dictCategories = JSONArray.parseArray(categoryArr.toJSONString(), DictCategory.class);
            if (CollectionUtils.isEmpty(dictCategories)) return;

            dictCategoryDao.deleteBy(dictCategories.stream().map(DictCategory::getCategoryKey).collect(Collectors.toList()));

            dictCategoryDao.save(dictCategories);
        } catch (Exception e) {
            LOGGER.error("保存字典列表错误", e);
        }
    }

    public void saveDictCategoryItem(String categoryItems) {
        if (StringUtils.isEmpty(categoryItems)) return;
        String categoryItemsText = SecurityUtil.decrypt3DES(token, categoryItems);
        try {
            JSONObject categoryItemObj = JSON.parseObject(categoryItemsText);
            if (categoryItemObj == null) return;
            JSONArray categoryItemArr = categoryItemObj.getJSONArray("dictCategoryItem");
            if (CollectionUtils.isEmpty(categoryItemArr)) return;

            //字典项存储
            List<DictCategoryItem> items = JSONArray.parseArray(categoryItemArr.toJSONString(), DictCategoryItem.class);
            if (CollectionUtils.isEmpty(items)) return;

            dictCategoryDao.deleteByDictCategoryItem(items);

            dictCategoryDao.saveDictCategoryItem(items);
        } catch (Exception e) {
            LOGGER.error("保存字典列表错误", e);
        }

    }

    public boolean existsDictCategory(String categoryCode) {
        if (StringUtils.isEmpty(categoryCode)) return false;
        return dictCategoryDao.existsDictCategory(categoryCode);
    }

    public String getDictCategory(String categoryCode) {

        List<DictCategoryItem> dictCategoryItems = dictCategoryDao.getDictCategoryItems(categoryCode);

        if (CollectionUtils.isEmpty(dictCategoryItems)) return StringUtils.EMPTY;
        JSONArray dictCategoryJsons = new JSONArray();
        dictCategoryItems.forEach(item -> dictCategoryJsons.add(JSON.parseObject(JSON.toJSONString(item))));

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", dictCategoryJsons.size());
        jsonObject.put("rows", dictCategoryJsons);
        return SecurityUtil.encrypt3DES(token, JSON.toJSONString(jsonObject));

    }
}
